package com.juicygames.undoer.player;

import android.util.Log;

import com.juicygames.framework.exception.GamePartNotFound;
import com.juicygames.framework.gamepart.GameHandler;
import com.juicygames.framework.gamepart.GamePart;
import com.juicygames.framework.property.BooleanProperty;
import com.juicygames.framework.property.space.CircleProperty;
import com.juicygames.framework.property.space.XVelocity;
import com.juicygames.framework.property.space.YVelocity;
import com.juicygames.framework.type.Circle;
import com.juicygames.undoer.ball.Ball;
import com.juicygames.undoer.ball.BallGroup;
import com.juicygames.undoer.ball.WallCollideAction;
import com.juicygames.undoer.player.property.CanMakeShadow;

public class WallCollideHandler extends GameHandler<WallCollideAction> {
	private static final String LOGTAG = WallCollideHandler.class.getName();

	public WallCollideHandler(GamePart self) {
		super(self);
		setActionClassHandled(WallCollideAction.class);
	}
	
	public void handle(WallCollideAction action) {
		Log.i(LOGTAG,"zomg. handling wall collide action");
		
		try {
			
			makeBall(getWorld().getPart(BallGroup.class));
			
		} catch (GamePartNotFound e) {
			
			BallGroup balls = getWorld().addPart(new BallGroup(getWorld()));
			makeBall(balls);
		}
	}
	
	private void makeBall(BallGroup balls) {
		BooleanProperty canMakeShadow = self.getPart(CanMakeShadow.class);
		if (canMakeShadow.get()) {
			Ball ball = new Ball(balls);
			balls.add(ball);
			ball.getPart(XVelocity.class).set(self.getPart(XVelocity.class).get());
			ball.getPart(YVelocity.class).set(self.getPart(YVelocity.class).get());
			ball.getPart(CircleProperty.class).set(new Circle(self.getPart(CircleProperty.class).get()));
			canMakeShadow.set(false);
		}
	}

}
